'use strict';
// configure default path root
require('./absolute-path');
var SCHEDULE = require('./libs/redis-schedule');



// let 'babel-polyfill';

let config = require('./config');
var scnedule = global.schedule = new SCHEDULE(config.schedule);


require('libs/Models');

let express = require('express');
let bodyParser = require('body-parser');
console.time(`https service started at ${config.server.port}`);
const app = express();

let SessionUser = require('middleware/session-user.mid');

require('./libs/logger')(app);

// app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.use(bodyParser.urlencoded({
  'extended': true
}));
app.use(bodyParser.json());
app.use(bodyParser.text({
  'defaultCharset': 'utf-8'
}));

app.use(SessionUser);

let co = require('co');
let ApiErrorHandler = require('middleware/error-handle.mid');


co(function*() {

  yield require('libs/mongo')(config.mongodb);

  require('./routes')(app, config);

  let https = require('http');
  let server = https.createServer(app);

  app.use(ApiErrorHandler);

  server.listen(config.server.port);

  require('./service/init-notify');

  console.timeEnd(`https service started at ${config.server.port}`);
}).catch(e => {
  console.log(e);
});
